/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.apiserver.http;

import org.openislands.oi.apiserver.app.JobService;
import org.openislands.oi.constant.Dict;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.dto.CommonResult;
import org.openislands.oi.pojo.dto.JobDTO;
import org.openislands.oi.pojo.qo.JobQO;
import org.openislands.oi.pojo.qo.JobSubmitQO;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.JobVO;
import org.openislands.oi.rpc.Generic;
import org.openislands.oi.rpc.RPCExecutor;
import org.openislands.oi.util.ValueCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/jobs")
public class JobController {
    @Resource
    private JobService jobService;
    @Resource
    private RPCExecutor rpcExecutor;

    @PostMapping("/")
    public CommonResponse<JobVO> submitJob(@RequestBody @Validated JobSubmitQO submitQO) {
        CommonResult<JobVO> masterResponse = rpcExecutor.internal(Dict.MASTER_MARK, new Generic<org.openislands.oi.master.http.JobController, CommonResponse<JobVO>>(
                jobController -> jobController.submitJob(submitQO)) {
        });
        return CommonResponse.parse(masterResponse);
    }

    @PutMapping("/")
    public CommonResponse<List<JobVO>> queryJob(@RequestBody JobQO jobQO){
        try{
            List<JobDTO> jobs = jobService.queryJob(ValueCopyUtils.copyProperties(jobQO, new JobDTO()));
            if (jobs.isEmpty()){
                return CommonResponse.error(MessageCodeEnum.DATA_NOT_FOUND_ERROR);
            }
            return CommonResponse.success(ValueCopyUtils.copyList(jobs, job -> ValueCopyUtils.copyProperties(job, new JobVO())));
        }catch (LogicException e){
            throw e;
        }
    }

    @PutMapping("/{jobId}/handle")
    public CommonResponse<Boolean> handleJob(@PathVariable(value = "jobId") String jobId) {
        CommonResult<Boolean> masterResponse = rpcExecutor.internal(Dict.MASTER_MARK, new Generic<org.openislands.oi.master.http.JobController, CommonResponse<Boolean>>(
                jobController -> jobController.handleJob(jobId)) {
        });
        return CommonResponse.parse(masterResponse);
    }

    @PutMapping("/scheduling")
    public CommonResponse<Boolean> schedulingJobs() {
        CommonResult<Boolean> masterResponse = rpcExecutor.internal(Dict.MASTER_MARK, new Generic<org.openislands.oi.master.http.JobController, CommonResponse<Boolean>>(
                jobController -> jobController.schedulingJobs()) {
        });
        return CommonResponse.parse(masterResponse);
    }

    @PutMapping("/{jobId}/stop")
    public CommonResponse<Boolean> stopJob(@PathVariable(value = "jobId") String jobId) {
        CommonResult<Boolean> masterResponse = rpcExecutor.internal(Dict.MASTER_MARK, new Generic<org.openislands.oi.master.http.JobController, CommonResponse<Boolean>>(
                jobController -> jobController.stopJob(jobId)) {
        });
        return CommonResponse.parse(masterResponse);
    }
}
